<?php
/**
 * Created by PhpStorm.
 * User: niechenguang
 * Date: 2016-12-19
 * Time: 0:02
 */

namespace app\library;

use huanxin\Easemob;

class HuanXin
{

    private static $objEasemob;

    private static $initParam = [
        'org_name' => '1142170214115289',
        'app_name' => 'mansheapp',
        'client_id' => 'YXA6HS6U4PKgEeaN0l1ilof9Ag',
        'client_secret' => 'YXA66UC1unefUPp4syOiPpjumj45FXc',
    ];

    /**
     * 获取token
     *
     * @return mixed
     */
    public static function getToken()
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return explode(' ', self::$objEasemob->getToken())[1];
    }

    /**
     * 创建用户
     *
     * @param $username
     * @param $passwd
     * @return mixed
     */
    public static function createUser($username, $passwd)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return self::$objEasemob->createUser($username, $passwd);
    }

    /**
     * 创建一组用户
     *
     * [
     *     [
     *         "username" => "zhangsan",
     *         "password" => "123456"
     *     ],
     *     [
     *         "username" => "lisi",
     *         "password" => "123456"
     *     ]
     * ]
     *
     * @param $arrUser
     * @return mixed
     */
    public static function createUsers($arrUser)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return self::$objEasemob->createUsers($arrUser);
    }

    /**
     * 获取用户信息
     *
     * @param $username
     * @return mixed
     */
    public static function getUser($username)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return self::$objEasemob->getUser($username);
    }

    /**
     * username添加friendname为好友
     *
     * @param $username
     * @param $friendname
     * @return mixed
     */
    public static function addFriend($username, $friendname)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return self::$objEasemob->addFriend($username, $friendname);
    }

    /**
     * 删除好友
     *
     * @param $username
     * @param $friendname
     * @return mixed
     */
    public static function delFriend($username, $friendname)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return self::$objEasemob->deleteFriend($username, $friendname);
    }

    /**
     * 获取好友
     *
     * @param $username
     * @return mixed
     */
    public static function getFriends($username)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return self::$objEasemob->showFriends($username);
    }

    /**
     * 修改昵称
     *
     * @param $username
     * @param $nickname
     * @return mixed
     */
    public static function modify($username, $nickname)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return self::$objEasemob->editNickname($username, $nickname);
    }

    /**
     * 获取黑名单
     *
     * @param $username
     * @return mixed
     */
    public static function getBlacklist($username)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return self::$objEasemob->getBlacklist($username);
    }

    /**
     * 添加黑名单
     *
     * @param $username
     * @param $username_black ['user1', 'user2']
     * @return mixed
     */
    public static function addUserForBlacklist($username, $username_black)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        $usernames = array(
            "usernames" => $username_black
        );
        return self::$objEasemob->addUserForBlacklist($username, $usernames);
    }

    /**
     * 把用户从黑名单中剔除
     *
     * @param $username
     * @param $username_black
     * @return mixed
     */
    public static function delUserForBlacklist($username, $username_black)
    {
        self::$objEasemob = new Easemob(self::$initParam);
        return self::$objEasemob->deleteUserFromBlacklist($username, $username_black);
    }

}